const db = require('../dbbase/db');
const uid = require("node-uuid");
const moment = require('moment');

//提交设备点检配置内容
exports.applyCheckContentConfig = async (req, res, next) => {
    try {
        const date = moment().format('YYYY-MM-DD HH:mm:ss');
        const content = req.body.data;

        const sql = "select c.checkID checkID from check_content_config c where checkName='"+ content.checkName +"' ";
        const params = [];
        const checkid = await db.query(sql,params);

        if(checkid.length === 0){
            const checkID = uid.v1();
            for(const item of content.tableCell){
                const sql = "insert into check_content_config (checkID,checkName,deviceName,deviceID,content,createTime,farmName,farmID,checkState,orderID,orderName) values(?,?,?,?,?,?,?,?,?,?,?) ";
                const params = [checkID,content.checkName,content.deviceName,content.deviceID,item.text,date,content.farmName,content.farmID,'1',content.orderID,content.orderName];
                db.query(sql, params).then((result) => {
                    res.status(200).json(result)
                }).catch((err) => {
                    console.log(err);
                });
            }
        }else {
            const checkID = checkid[0].checkID;
            for(const item of content.tableCell){
                const sql = "insert into check_content_config (checkID,checkName,deviceName,deviceID,content,createTime,farmName,farmID,checkState,orderID,orderName) values(?,?,?,?,?,?,?,?,?,?,?) ";
                const params = [checkID,content.checkName,content.deviceName,content.deviceID,item.text,date,content.farmName,content.farmID,'1',content.orderID,content.orderName];
                db.query(sql, params).then((result) => {
                    res.status(200).json(result)
                }).catch((err) => {
                    console.log(err);
                });
            }
        }
    } catch (err) {
        next(err)
    }
}

//获取设备相应的点检内容列表
exports.getDeviceCheckContentList = async (req, res, next) => {
    try {
        console.log(req.body);
        const farmID = req.body.device.farmID;

        if(farmID === "1"){
            const deviceId = req.body.device.deviceID;
            const orderId = req.body.device.orderID;
            const sql = "select * from check_content_config where deviceID='" + deviceId + "' and orderID='" + orderId + "'";
            const params = [];
            db.query(sql, params).then((result) => {
                res.status(200).json(result)
            }).catch((err) => {
                console.log(err);
            });
        }else{
            const deviceId = req.body.device.deviceID;
            const sql = "select * from check_content_config where deviceID='" + deviceId + "'";
            const params = [];
            db.query(sql, params).then((result) => {
                res.status(200).json(result)
            }).catch((err) => {
                console.log(err);
            });
        }
    } catch (err) {
        next(err)
    }
}
//获取设备相应的点检记录
exports.getCheckInfoList = async (req, res, next) => {
    try {
        const sql = "select * from check_list ";
        const params = [];
        db.query(sql, params).then((result) => {
            res.status(200).json(result)
        }).catch((err) => {
            console.log(err);
        });
    } catch (err) {
        next(err)
    }
}
//获取设备相应的点检记录
exports.getDeviceCheckContentDetail = async (req, res, next) => {
    try {
        const contentId = req.body.contentId;
        const sql = "select * from check_content where contentID='"+ contentId +"' ";
        const params = [];
        db.query(sql, params).then((result) => {
            res.status(200).json(result)
        }).catch((err) => {
            console.log(err);
        });
    } catch (err) {
        next(err)
    }
}